<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input
        v-model="listQuery.checkIndexName"
        clearable
        class="filter-item"
        style="width: 200px"
        placeholder="请输入名称"
      />
      <el-button
        v-permission="['GET /admin/service/type/list']"
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >查找
      </el-button>
      <el-button
        v-permission="['POST /admin/service/type/add']"
        class="filter-item"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >添加
      </el-button>
    </div>

    <!-- 查询结果 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="正在查询中..."
      border
      fit
      highlight-current-row
    >
      <el-table-column
        width="140"
        align="center"
        label="ID"
        prop="id"
        sortable
      />

      <el-table-column align="center" label="风险评估指标名称" prop="checkIndexName"/>

      <el-table-column align="center" label="上限值" prop="highValue"/>
      <el-table-column align="center" label="下限值" prop="lowValue"/>
      <el-table-column align="center" label="单位" prop="unit"/>

      <el-table-column align="center" label="参考值" prop="normalVal"/>


      <el-table-column align="center" label="昊邦体检指标名称" prop="haopyIndexName"/>

      <el-table-column align="center" label="检査项类型" prop="resultType">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.resultType == 0">未知</el-tag>
          <el-tag v-else-if="scope.row.resultType == 1">数值型</el-tag>
          <el-tag v-else-if="scope.row.resultType == 2">阴阳定性型</el-tag>
          <el-tag v-else-if="scope.row.resultType == 3">半定量半定性型</el-tag>
          <el-tag v-else type="info">文本型</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        label="操作"
        width="300"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-permission="['GET /admin/service/type/update']"
            type="primary"
            size="mini"
            @click="handleUpdate(scope.row)"
          >编辑
          </el-button>

          <el-button
            v-permission="['POST /admin/service/type/delete']"
            type="danger"
            size="mini"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="dataForm"
        status-icon
        label-position="left"
        label-width="140px"
        style="width: 700px; margin-left: 50px"
      >
        <el-form-item label="检查项名称" prop="checkIndexName">
          <el-input v-model="dataForm.checkIndexName"/>
        </el-form-item>

        <el-form-item label="上限值" prop="highValue">
          <el-input v-model="dataForm.highValue"/>
        </el-form-item>
        <el-form-item label="下限值" prop="lowValue">
          <el-input v-model="dataForm.lowValue"/>
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="dataForm.unit"/>
        </el-form-item>

        <el-form-item label="参考范围" prop="normalVal">
          <el-input v-model="dataForm.normalVal"/>
        </el-form-item>

        <el-form-item label="昊邦指标名称" prop="haopyIndexName">
          <el-input v-model="dataForm.haopyIndexName"/>
        </el-form-item>

        <el-form-item label="检査项类型" prop="resultType">
          <el-radio-group v-model="dataForm.resultType">
            <el-radio :label="0">未知</el-radio>
            <el-radio :label="1">数值型</el-radio>
            <el-radio :label="2">阴阳定性型</el-radio>
            <el-radio :label="3">半定量半定性型</el-radio>
            <el-radio :label="4">文本型</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button
          v-if="dialogStatus == 'create'"
          type="primary"
          @click="createData"
        >确定
        </el-button>
        <el-button
          v-else
          type="primary"
          :loading="saveLoading"
          @click="updateData"
        >确定
        </el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<style>
</style>

<script>
  import {
    indexList,
    indexDelete,
    indexCreate,
    indexUpdate
  } from '@/api/topic_answer'
  import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

  export default {
    name: 'CheckIndex',
    components: { Pagination },
    data() {
      return {
        list: [],
        total: 0,
        listLoading: true,
        listQuery: {
          page: 1,
          name: undefined,
          sort: '',
          order: '',
          limit: 20
        },
        dataForm: {
          id: undefined,
          checkIndexName: undefined,
          highValue: '',
          lowValue: '',
          normalVal: '',
          unit: '',
          resultType: undefined,
          haopyIndexName: ''
        },
        dialogFormVisible: false,
        dialogStatus: '',
        textMap: {
          update: '编辑',
          create: '创建'
        },
        saveLoading: false,
        rules: {
          checkIndexName: [{ required: true, message: '不能为空', trigger: 'blur' }]
        }
      }
    },
    computed: {},
    created() {
      this.getList()
    },
    methods: {
      getList() {
        this.listLoading = true
        indexList(this.listQuery)
          .then((response) => {
            this.list = response.data.data.list
            this.total = response.data.data.total
            this.listLoading = false
          })
          .catch(() => {
            this.list = []
            this.total = 0
            this.listLoading = false
          })
      },
      handleFilter() {
        this.listQuery.page = 1
        this.getList()
      },
      resetForm() {
        this.dataForm = {
          id: undefined,
          checkIndexName: undefined,
          highValue: '',
          lowValue: '',
          normalVal: '',
          unit: '',
          resultType: undefined,
          haopyIndexName: ''

        }
      },
      handleCreate() {
        this.resetForm()
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      createData() {
        let that = this
        console.log(this.dataForm)
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            that.saveLoading = true
            indexCreate(this.dataForm)
              .then((response) => {
                that.saveLoading = false
                that.getList()
                this.dialogFormVisible = false
                this.$notify.success({
                  title: '成功',
                  message: '添加成功'
                })
              })
              .catch((response) => {
                that.saveLoading = false
                this.$notify.error({
                  title: '失败',
                  message: response.data.errmsg
                })
              })
          }
        })
      },
      handleUpdate(row) {
        this.dataForm = Object.assign({}, row)
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      updateData() {
        console.log(this.dataForm)
        let that = this
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            that.saveLoading = true
            indexUpdate(this.dataForm)
              .then(() => {
                that.saveLoading = false
                that.getList()
                this.dialogFormVisible = false
                this.$notify.success({
                  title: '成功',
                  message: '更新成功'
                })
              })
              .catch((response) => {
                that.saveLoading = false
                this.$notify.error({
                  title: '失败',
                  message: response.data.errmsg
                })
              })
          }
        })
      },
      handleDelete(row) {
        indexDelete(row.id)
          .then((response) => {
            this.$notify.success({
              title: '成功',
              message: '删除成功'
            })
            this.getList()
          })
          .catch((response) => {
            this.$notify.error({
              title: '失败',
              message: response.data.errmsg
            })
          })
      }
    }
  }
</script>
